package com.atguigu.day10;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink02_SQL_GroupWindow {
    public static void main(String[] args) {
        //1.获取相关环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        tableEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int, " +
                "t as to_timestamp(from_unixtime(ts/1000,'yyyy-MM-dd HH:mm:ss'))," +
                "watermark for t as t - interval '5' second)" +
                "with("
                + "'connector' = 'filesystem',"
                + "'path' = 'input/sensor-sql.txt',"
                + "'format' = 'csv'"
                + ")");


        //TODO 2.使用groupWindow
//        tableEnv.executeSql("select " +
//                "id," +
//                "sum(vc)," +
//                "TUMBLE_start(t, INTERVAL '3' second) as wStart," +
//                "TUMBLE_end(t, INTERVAL '3' second) as wEnd" +
//                " from sensor " +
//                "group by id,TUMBLE(t, INTERVAL '3' second)").print();
        tableEnv.executeSql("select " +
                "id," +
                "sum(vc)," +
                "hop_start(t, INTERVAL '2' second ,interval '3' second) as wStart," +
                "hop_end(t, INTERVAL '2' second ,interval '3' second) as wEnd" +
                " from sensor " +
                "group by id,hop(t, INTERVAL '2' second, interval '3' second)").print();
    }
}
